package com.chinaunicom.member.trigger.rpc;

import com.alibaba.fastjson.JSONObject;
import com.chinaunicom.api.dto.MemberDTO;
import com.chinaunicom.api.IMemberFacadeApi;
import com.chinaunicom.common.constants.Constants;
import com.chinaunicom.common.result.JsonResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.data.redis.core.StringRedisTemplate;

import javax.annotation.Resource;

/**
 * @author guomenghuai
 * @date 2024/1/8
 * @Description
 */
@Service
public class MemberFacadeApi implements IMemberFacadeApi {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public JsonResult<MemberDTO> info(String token) {

        if (StringUtils.isBlank(token)) {
            throw new RuntimeException("token不能为空");
        }

        if (!stringRedisTemplate.hasKey(Constants.UserCacheConstants.YJWL_MEMBER_INFO_KEY + token)) {
            throw new RuntimeException("token不正确");
        }

        String memberInfo = stringRedisTemplate.opsForValue().get(Constants.UserCacheConstants.YJWL_MEMBER_INFO_KEY + token);
        if (StringUtils.isBlank(memberInfo)) {
            throw new RuntimeException("登录已过期，请重新登录");
        }

        MemberDTO userDTO = JSONObject.parseObject(memberInfo, MemberDTO.class);

        return JsonResult.success(userDTO);
    }

}
